/*
 * DetailJadwal.java
 *
 * Created on April 18, 2007, 1:51 PM
 */

package org.wii.jadwal.gui;

import javax.swing.JOptionPane;
import org.wii.jadwal.model.Jadwal;
import org.wii.jadwal.model.JadwalManager;

/**
 *
 * @author  Hendro
 */
public class DetailJadwal extends javax.swing.JFrame {
    Jadwal jadwal;
    /** Creates new form DetailJadwal */
    public DetailJadwal() {
        initComponents();
        this.jadwal = jadwal;
    }
    
    public DetailJadwal(Jadwal jadwal) {
        initComponents();
        this.jadwal = jadwal;
        bindData();
    }
    
    private void bindData(){
        this.txtPostDate.setText(jadwal.getPostDate().toString());
        this.txtWarningDate.setText(jadwal.getWarningDate().toString());
        this.txtTitle.setText(jadwal.getTitle());
        this.txtKonten.setText(jadwal.getContent());
        if(jadwal.getStatus()==0)txtStatus.setText("Not Complete");
        else txtStatus.setText("Completed");
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtPostDate = new javax.swing.JTextField();
        txtWarningDate = new javax.swing.JTextField();
        txtTitle = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtKonten = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        txtStatus = new javax.swing.JTextField();
        btnTutup = new javax.swing.JButton();
        btnCompleted = new javax.swing.JButton();
        btnUbah = new javax.swing.JButton();
        btnSimpan = new javax.swing.JButton();
        btnHapus = new javax.swing.JButton();

        setTitle("Detail Jadwal");
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Detail Jadwal"));
        jLabel1.setText("Posting Date :");

        jLabel2.setText("Warning Date :");

        jLabel3.setText("Judul :");

        jLabel4.setText("Konten :");

        txtPostDate.setEditable(false);

        txtWarningDate.setEditable(false);

        txtTitle.setEditable(false);

        txtKonten.setColumns(20);
        txtKonten.setEditable(false);
        txtKonten.setRows(5);
        jScrollPane1.setViewportView(txtKonten);

        jLabel5.setText("Status :");

        txtStatus.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .addComponent(txtTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtWarningDate)
                        .addComponent(txtPostDate, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtPostDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtWarningDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(30, 30, 30))
        );

        btnTutup.setText("Tutup");
        btnTutup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTutupActionPerformed(evt);
            }
        });

        btnCompleted.setText("Completed Jadwal");
        btnCompleted.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCompletedActionPerformed(evt);
            }
        });

        btnUbah.setText("Ubah");
        btnUbah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUbahActionPerformed(evt);
            }
        });

        btnSimpan.setText("Simpan");
        btnSimpan.setEnabled(false);
        btnSimpan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSimpanActionPerformed(evt);
            }
        });

        btnHapus.setText("Hapus");
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnUbah)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSimpan)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnHapus)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 123, Short.MAX_VALUE)
                        .addComponent(btnCompleted)
                        .addGap(5, 5, 5)
                        .addComponent(btnTutup)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCompleted)
                    .addComponent(btnTutup)
                    .addComponent(btnSimpan)
                    .addComponent(btnUbah)
                    .addComponent(btnHapus))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
// TODO add your handling code here:
        JadwalManager mgr = new JadwalManager();
        try {
            mgr.deleteJadwal(jadwal);
            this.txtPostDate.setText("");
            this.txtWarningDate.setText("");
            this.txtTitle.setText("");
            this.txtKonten.setText("");
            this.txtStatus.setText("");
            btnHapus.setEnabled(false);
            btnUbah.setEnabled(false);
            btnCompleted.setEnabled(false);
            JOptionPane.showMessageDialog(this,"Data terhapus");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,"Data gagal dihapus");
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnHapusActionPerformed
    
    private void btnSimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSimpanActionPerformed
        this.jadwal.setTitle(txtTitle.getText());
        this.jadwal.setContent(txtKonten.getText());
        JadwalManager mgr = new JadwalManager();
        try {
            mgr.updateJadwal(jadwal);
            bindData();
            JOptionPane.showMessageDialog(this,"Data tersimpan");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,"Data gagal diupdate");
            ex.printStackTrace();
        }
        txtTitle.setEditable(false);
        txtKonten.setEditable(false);
        btnSimpan.setEnabled(false);
        btnUbah.setText("Ubah");
        bindData();
    }//GEN-LAST:event_btnSimpanActionPerformed
    
    private void btnUbahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUbahActionPerformed
        if(btnUbah.getText().equalsIgnoreCase("Ubah")){
            txtTitle.setEditable(true);
            txtKonten.setEditable(true);
            txtTitle.requestFocus();
            btnUbah.setText("Batal");
            btnSimpan.setEnabled(true);
        }else{
            txtTitle.setEditable(false);
            txtKonten.setEditable(false);
            btnSimpan.setEnabled(false);
            btnUbah.setText("Ubah");
            bindData();
        }
    }//GEN-LAST:event_btnUbahActionPerformed
    
    private void btnTutupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTutupActionPerformed
        // TODO add your handling code here:
        this.hide();
    }//GEN-LAST:event_btnTutupActionPerformed
    
    private void btnCompletedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCompletedActionPerformed
        jadwal.setStatus(1);
        JadwalManager mgr = new JadwalManager();
        try {
            mgr.updateJadwal(jadwal);
            bindData();
            JOptionPane.showMessageDialog(this,"Data tersimpan");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,"Data gagal diupdate");
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnCompletedActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DetailJadwal().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCompleted;
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnSimpan;
    private javax.swing.JButton btnTutup;
    private javax.swing.JButton btnUbah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtKonten;
    private javax.swing.JTextField txtPostDate;
    private javax.swing.JTextField txtStatus;
    private javax.swing.JTextField txtTitle;
    private javax.swing.JTextField txtWarningDate;
    // End of variables declaration//GEN-END:variables
    
}
